package main.java.cn.wolfcode.web.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletionService;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IConsumptionItemService consumptionItemService;


    @RequiredPermission(name="结算单页面",expression="consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumption/list";
    }


    @RequiredPermission(name="结算单删除",expression="consumption:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           consumptionService.delete(id);
        }
        return "redirect:/consumption/list";
    }


    @RequiredPermission(name="结算单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption) {
        if (consumption.getId() == null) {
            consumptionService.save(consumption);
        } else {
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }


    @RequiredPermission(name="结算单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        model.addAttribute("businesses",businessService.listAll());
        if (id != null) {
            Consumption consumption = consumptionService.get(id);
            model.addAttribute("consumption",consumption);
            //根据结算单流水号查询结算单明细数据
         List<ConsumptionItem> consumptionItems= consumptionItemService.selectByCno(consumption.getCno());
         model.addAttribute("items",consumptionItems);
        }else {
            //这里处理结算单明细回显的问题
            Consumption consumption = new Consumption();
            //创建流水号
            Date date = new Date();
            String format = DateUtil.format(date, "yyyyMMddHHssmm");
            String s = RandomUtil.randomNumbers(5);
            consumption.setCno(format+s);
            consumptionService.save(consumption);
            return "redirect:/consumption/input?id="+consumption.getId();
        }

        return "consumption/input";
    }

    @RequiredPermission(name="结算单新增",expression="appointment:save")
    @RequestMapping("/save")
    public String save(String ano) {
       Consumption consumption= consumptionService.save(ano);
        return "redirect:/consumption/input?id="+consumption.getId();
    }
}
